package com.gzy.rpsm_property.mapper;

import com.github.pagehelper.Page;
import com.gzy.rpsm_property.pojo.Amount;
import com.gzy.rpsm_property.pojo.Message;
import com.gzy.rpsm_property.pojo.Ms;
import com.gzy.rpsm_property.pojo.User;

import com.gzy.rpsm_property.pojo.vo.UnpaidOwner;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    @Insert("insert into user value(NULL,#{account},#{password},#{idcard},#{username},#{gender},#{age},#{user_type},#{user_status},#{user_roomid},NULL)")
    int insertUser(User user);

    Page<User> owners();

    @Update("update user set username = #{username},account=#{account},password=#{password},age=#{age},gender=#{gender},idcard=#{idcard} where userid = #{userid}")
    int changeUser(User user);

    @Select("select userid,username,account,idcard,gender,age,user_type,user_status,user_roomid from user where userid = #{id}")
    User queryById(int id);

    @Select("select a.amount_date,a.amount,`user`.userid,`user`.username,room.room_code from (select * from amount" +
            " where amount.isPaied = 2 and amount.amount_type =#{typeId}) as a" + " LEFT JOIN room on a.amount_roomid = room.room_id" +
            " left join user on room.room_id =`user`.user_roomid" +" order by a.amount_date asc ,`user`.username")
    @ResultMap("com.gzy.rpsm_property.mapper.UserMapper.unpaidOwnerList")
    List<UnpaidOwner> unpaidOwnerList(Integer userId);



    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);
//
//    @Select("SELECT " +
//            "    amount_id, " +
//            "    amount_roomid, " +
//            "    amount_type, " +
//            "    amount_date, " +
//            "    amount, " +
//            "    isPaied " +
//            "FROM " +
//            "    amount " +
//            "WHERE " +
//            "    amount_roomid = #{roomId} " +
//            "    AND isPaied = 2 " +
//            "ORDER BY " +
//            "    amount_date ASC")
////    @ResultMap("com.gzy.rpsm_property.mapper.UserMapper.unpaidAmountList")
//    List<UnpaidAmount> getUnpaidAmountsByRoomId(@Param("roomId") Integer roomId);
@Select("SELECT " +
        "    amount_id, " +
        "    amount_roomid, " +
        "    amount_type, " +
        "    amount_date, " +
        "    amount, " +
        "    isPaied " +
        "FROM " +
        "    amount " +
        "WHERE " +
        "    amount_roomid = #{roomId} " +
        "    AND isPaied = 2 " +
        "ORDER BY " +
        "    amount_date ASC")
List<Amount> getUnpaidAmountsByRoomId(@Param("roomId") Integer roomId);
    @Select("select userid,username,account,idcard,gender,age,user_type,user_status,user_roomid from user where account = #{account} and password = #{password} and user_type = 3")
            User queryByAccountAndPassword(String account, String password);

    @Update("UPDATE amount SET isPaied = 1, amount = 0 WHERE amount_id = #{amountId}")
    int payAmount(@Param("amountId") int amountId);
    List<Ms> getAllMessagesWithUserDetails();


    @Select("SELECT * FROM user WHERE username = #{username}")
    User getUserByUsername(@Param("username") String username);
    User getUserByRoomIdAndUsername(Integer roomId, String username);
    @Update("UPDATE user SET user_status = 31 WHERE userid = #{userid}")
    int activateUser(int userid);
}


   